unit MainFrm;

interface

uses
  Classes, SysUtils, Windows, StrUtils, Menus, ImgList, Controls, StdCtrls, ComCtrls, ToolWin, ExtCtrls,
  ActnList, StdActns, AppEvnts, XPMan, Graphics, Variants,
  DB, DBClient, DBCtrls, MConnect,
  Forms, BaseFrm, DBBaseFrm,
  PoslovanjeSrv_TLB, GlobalSettings,
  cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxEdit, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxVGrid, ActnMan, ActnCtrls, ActnMenus,
  XPStyleActnCtrls, StdStyleActnCtrls,
  alActions;

type
  TWinControlCrack = class(TWinControl);

  TfrmMain = class(TForm)
    imglstMain: TImageList;
    stbMain: TStatusBar;
    mnuMain: TMainMenu;
    mnuMainFile: TMenuItem;
    mnuMainWindow: TMenuItem;
    mnuMainHelp: TMenuItem;
    mnuMainFileConnect: TMenuItem;
    mnuMainFileDisconnect: TMenuItem;
    mnuMainFileS1: TMenuItem;
    mnuMainFileExit: TMenuItem;
    mnuMainHelpAbout: TMenuItem;
    actnlstMain: TActionList;
    actnFileConnect: TAction;
    actnFileDisconnect: TAction;
    actnFileExit: TAction;
    xpmnfstMain: TXPManifest;
    mnuMainWindowMinimizeAll: TMenuItem;
    mnuMainWindowCascade: TMenuItem;
    appevntMain: TApplicationEvents;
    actnArtikelEdit: TAction;
    actnDrzavaEdit: TAction;
    actnPostaEdit: TAction;
    actnPoslovniPartnerEdit: TAction;
    actnTemeljnicaEdit: TAction;
    actnDnevnikEdit: TAction;
    actnGlavnaKnjigaEdit: TAction;
    actnSkupinaArtikelEdit: TAction;
    actnDDVEdit: TAction;
    actnEnotaMereEdit: TAction;
    actnValutaEdit: TAction;
    actnTemeljnicaVrstaEdit: TAction;
    actnPravniStatusEdit: TAction;
    actnPoslovnoLetoEdit: TAction;
    actnPodjetjeEdit: TAction;
    actnKontoEdit: TAction;
    cbMain: TControlBar;
    actnDBInsert: TAction;
    imglstNavigator: TImageList;
    tbNavigator: TToolBar;
    btnNavigatorFirst: TToolButton;
    btnNavigatorPrevious: TToolButton;
    btnNavigatorNext: TToolButton;
    btnNavigatorLast: TToolButton;
    btnNavigatorInsert: TToolButton;
    btnNavigatorDelete: TToolButton;
    btnNavigatorEdit: TToolButton;
    btnNavigatorPost: TToolButton;
    btnNavigatorCancel: TToolButton;
    btnNavigatorRefresh: TToolButton;
    actnDBFirst: TAction;
    actnDBNext: TAction;
    actnDBLast: TAction;
    actnDBPrior: TAction;
    actnDBDelete: TAction;
    actnDBEdit: TAction;
    actnDBPost: TAction;
    actnDBCancel: TAction;
    actnDBRefresh: TAction;
    actnlstNavigator: TActionList;
    imglstPoslovanje: TImageList;
    actnlstPoslovanje: TActionList;
    cnMain: TDCOMConnection;
    actnKnjigaIzdanihRacunovEdit: TAction;
    actnKnjigaPrejetihRacunovEdit: TAction;
    actnSkupinaPoslovniPartnerEdit: TAction;
    actnSkupinaTemeljnicaEdit: TAction;
    actnDelavecEdit: TAction;
    actnOsnovnoSredstvoEdit: TAction;
    mnuMainWindowCloseAll: TMenuItem;
    mnuMainModule: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvo: TMenuItem;
    mnuMainModuleMaterialnoKnjigovodstvo: TMenuItem;
    mnuMainModuleSifranti: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvoTemeljnica: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvoDnevnik: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvoGlavnaKnjiga: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvoKnjigaIzdanihRacunov: TMenuItem;
    mnuMainModuleFinancnoKnjigovodstvoKnjigaPrejetihRacunov: TMenuItem;
    mnuMainModuleSifrantiArtikel: TMenuItem;
    mnuMainModuleSifrantiPoslovniPartner: TMenuItem;
    mnuMainModuleSifrantiDelavec: TMenuItem;
    mnuMainModuleSifrantiOsnovnoSredstvo: TMenuItem;
    mnuMainModuleSifrantiSkupinaArtikel: TMenuItem;
    mnuMainModuleSifrantiSkupinaPoslovniPartner: TMenuItem;
    mnuMainModuleSifrantiSkupinaTemeljnica: TMenuItem;
    mnuMainModuleSifrantiPravniStatus: TMenuItem;
    mnuMainModuleSifrantiPosta: TMenuItem;
    mnuMainModuleSifrantiDrzava: TMenuItem;
    mnuMainModuleSifrantiDDV: TMenuItem;
    mnuMainModuleSifrantiEnotaMere: TMenuItem;
    mnuMainModuleSifrantiValuta: TMenuItem;
    mnuMainWindowS1: TMenuItem;
    tbModule: TToolBar;
    btnModuleTemeljnica: TToolButton;
    btnModuleDnevnik: TToolButton;
    btnModuleGlavnaKnjiga: TToolButton;
    btnModuleKnjigaPrejetihRacunov: TToolButton;
    btnModuleKnjigaIzdanihRacunov: TToolButton;
    imglstHelp: TImageList;
    actnlstHelp: TActionList;
    imglstWindow: TImageList;
    actnlstWindow: TActionList;
    mnuMainModuleS1: TMenuItem;
    mnuMainModuleSettings: TMenuItem;
    mnuMainModuleSettingsPodjetje: TMenuItem;
    mnuMainModuleSettingsPoslovnoLeto: TMenuItem;
    mnuMainModuleSettingsKonto: TMenuItem;
    actnWindowCloseAll: TAction;
    actnWindowMinimizeAll: TWindowMinimizeAll;
    actnWindowCascade: TWindowCascade;
    actnHelpAbout: TAction;
    tbMain: TToolBar;
    btnFileConnect: TToolButton;
    btnFileDiconnect: TToolButton;
    btnFileS1: TToolButton;
    btnFileExit: TToolButton;
    btnNavigatorS1: TToolButton;
    btnNavigatorFilter: TToolButton;
    actnDBFilter: TAction;
    procedure actnEditExecute(Sender: TObject);
    procedure actnFileConnectExecute(Sender: TObject);
    procedure actnFileDisconnectExecute(Sender: TObject);
    procedure actnFileExitExecute(Sender: TObject);
    procedure actnNavigatorExecute(Sender: TObject);
    procedure actnNavigatorUpdate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure actnWindowCloseAllExecute(Sender: TObject);
    procedure actnWindowCloseAllUpdate(Sender: TObject);
  private
    FSession: ISession;
    function FindForm(FormClass: TfrmBaseClass; out Count: Integer): TfrmBase;
    function GetActiveForm: TForm;
    function GetServer: IrdmMain;
    procedure ActiveControlChange(Sender: TObject);
    //procedure ActivateActions(ActionList: TActionList; Index: Integer);
    function GetApplicationCaption: string;
  protected
    property Session: ISession read FSession;
    property ApplicationCaption: string read GetApplicationCaption;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function ShowForm(FormClass: TfrmBaseClass; MaxCount: Integer = 1): TfrmBase;
    property ActiveForm: TForm read GetActiveForm;
    property Server: IrdmMain read GetServer;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

uses
  PrijavaFrm, 
  ArtikelFrm, PoslovniPartnerFrm, DelavecFrm, OsnovnoSredstvoFrm,
  SkupinaArtikelFrm, SkupinaPoslovniPartnerFrm, SkupinaTemeljnicaFrm, 
  PravniStatusFrm, PostaFrm, DrzavaFrm, DDVFrm, EnotaMereFrm, ValutaFrm,
  PoslovnoLetoFrm, PodjetjeFrm, KontoFrm, TemeljnicaVrstaFrm,
  TemeljnicaFrm, DnevnikFrm, GlavnaKnjigaFrm,
  KnjigaIzdanihRacunovFrm, KnjigaPrejetihRacunovFrm;

{ TfrmMain }

procedure TfrmMain.ActiveControlChange(Sender: TObject);

  procedure SetColor(AControl: TWinControl; AColor: TColor);
  begin
    if AControl is TcxVerticalGrid then
      TWinControlCrack(ActiveForm.ActiveControl).Color := AColor
    else
      TWinControlCrack(AControl).Color := AColor;
  end;

  procedure UpdateActiveControl(Control: TWinControl);
  var
    i: Integer;
    ChildControl: TWinControl;
    CurrentControl: TWinControl;
  begin
    CurrentControl := Screen.ActiveControl;
    for i := 0 to Control.ControlCount - 1 do
    begin
      if (Control.Controls[I] is TWinControl) then
      begin
        ChildControl := Control.Controls[I] as TWinControl;
        if (not (ChildControl is TCustomPanel)) and (not (ChildControl is TCustomGroupBox)) then
        begin
          if (ChildControl = CurrentControl) or
            (Assigned(CurrentControl) and (ChildControl = CurrentControl.Parent))
          then
            SetColor(ChildControl, ActiveControlColor)
          else
            SetColor(ChildControl, InactiveControlColor);
        end
        else
          UpdateActiveControl(ChildControl);
      end;
    end;
  end;

begin
  if Assigned(ActiveForm) and not (csDestroying in ActiveForm.ComponentState) then
    UpdateActiveControl(ActiveForm);
end;

procedure TfrmMain.actnWindowCloseAllExecute(Sender: TObject);
var
  i: Integer;
begin
  for i := MDIChildCount - 1 downto 0 do
    MDIChildren[i].Close;
end;

procedure TfrmMain.actnWindowCloseAllUpdate(Sender: TObject);
begin
  if MDIChildCount > 0 then
    actnWindowCloseAll.Enabled := True
  else
    actnWindowCloseAll.Enabled := False;
end;

procedure TfrmMain.actnEditExecute(Sender: TObject);
begin
  // Financno poslovanje
  if Sender = actnTemeljnicaEdit then
    ShowForm(TfrmTemeljnica)
  else if Sender = actnDnevnikEdit then
    ShowForm(TfrmDnevnik)
  else if Sender = actnGlavnaKnjigaEdit then
    ShowForm(TfrmGlavnaKnjiga)
  else if Sender = actnKnjigaIzdanihRacunovEdit then
    ShowForm(TfrmKnjigaIzdanihRacunov)
  else if Sender = actnKnjigaPrejetihRacunovEdit then
    ShowForm(TfrmKnjigaPrejetihRacunov)
  // Sifranti
  else if Sender = actnArtikelEdit then
    ShowForm(TfrmArtikel)
  else if Sender = actnPoslovniPartnerEdit then
    ShowForm(TfrmPoslovniPartner)
  else if Sender = actnDelavecEdit then
    ShowForm(TfrmDelavec)
  else if Sender = actnOsnovnoSredstvoEdit then
    ShowForm(TfrmOsnovnoSredstvo)
  else if Sender = actnSkupinaArtikelEdit then
    ShowForm(TfrmSkupinaArtikel)
  else if Sender = actnSkupinaPoslovniPartnerEdit then
    ShowForm(TfrmSkupinaPoslovniPartner)
  else if Sender = actnSkupinaTemeljnicaEdit then
    ShowForm(TfrmSkupinaTemeljnica)
  else if Sender = actnPravniStatusEdit then
    ShowForm(TfrmPravniStatus)
  else if Sender = actnPostaEdit then
    ShowForm(TfrmPosta)
  else if Sender = actnDrzavaEdit then
    ShowForm(TfrmDrzava)
  else if Sender = actnDDVEdit then
    ShowForm(TfrmDDV)
  else if Sender = actnEnotaMereEdit then
    ShowForm(TfrmEnotaMere)
  else if Sender = actnValutaEdit then
    ShowForm(TfrmValuta)
  // Nastavitve
  else if Sender = actnPoslovnoLetoEdit then
    ShowForm(TfrmPoslovnoLeto)
  else if Sender = actnPodjetjeEdit then
    ShowForm(TfrmPodjetje)
  else if Sender = actnKontoEdit then
    ShowForm(TfrmKonto)
  else if Sender = actnTemeljnicaVrstaEdit then
    ShowForm(TfrmTemeljnicaVrsta)
  else
    ShowForm(TfrmPravniStatus);
end;

procedure TfrmMain.actnFileConnectExecute(Sender: TObject);
var
  Form: TfrmPrijava;
  Params: TParams;
  ModalResult: Integer;

  function GetParamValue(ASwitch: string): string;
  var
    i: Integer;
    CmdParam: string;
    DelimiterPos: Integer;
  begin
    Result := '';
    for i := 1 to ParamCount do
    begin
      CmdParam := ParamStr(i);
      DelimiterPos := Pos(':', CmdParam);
      if AnsiCompareText(LeftStr(CmdParam, DelimiterPos - 1), ASwitch) = 0 then
      begin
        Result := Copy(CmdParam, DelimiterPos + 1, MaxInt);
        Break;
      end;
    end;
  end;

  procedure SetGUIControls;
  begin
    actnFileConnect.Enabled := False;
    actnFileDisconnect.Enabled := True;
    tbModule.Visible := True;
    tbNavigator.Visible := True;
    mnuMainModule.Visible := True;
    mnuMainWindow.Visible := True;
  end;

begin
  try
    cnMain.Open;
    Form := TfrmPrijava.Create(nil);
    try
      Form.edtUporabniskoIme.Properties.Value := GetParamValue('/usr');
      Form.edtGeslo.Properties.Value := GetParamValue('/pwd');
      Form.lkpcbPodjetje.Properties.Value := StrToIntDef(GetParamValue('/PodjetjeID'), -1);
      Form.lkpcbPoslovnoLeto.Properties.Value := StrToIntDef(GetParamValue('/PoslovnoLetoID'), -1);
      ModalResult := Form.ShowModal;
      if ModalResult = mrOK then
      begin
        Params := TParams.Create;
        try
          Params.CreateParam(ftInteger, 'PODJETJE_ID', ptInput).Value :=
            Form.lkpcbPodjetje.Properties.Value;
          Params.CreateParam(ftInteger, 'PODJETJE', ptInput).Value :=
            Form.cdsMain.Lookup('ID', Form.lkpcbPodjetje.Properties.Value, 'NAZIV');
          Params.CreateParam(ftInteger, 'POSLOVNO_LETO_ID', ptInput).Value :=
            Form.lkpcbPoslovnoLeto.Properties.Value;
          Params.CreateParam(ftInteger, 'POSLOVNO_LETO', ptInput).Value :=
            Form.cdsPoslovnoLeto.Lookup('ID', Form.lkpcbPoslovnoLeto.Properties.Value, 'NAZIV');
          Params.CreateParam(ftInteger, 'DRZAVA_ID', ptInput).Value :=
            Form.cdsPosta.Lookup('ID', Form.cdsMain.FieldByName('POSTA_ID').Value, 'DRZAVA_ID');
          Params.CreateParam(ftBoolean, 'READ_ONLY', ptInput).Value :=
            StrToBoolDef(GetParamValue('/ReadOnly'), False);
          Server.Prijava(Form.edtUporabniskoIme.Properties.Value, Form.edtGeslo.Properties.Value,
            PackageParams(Params), FSession);
          if Assigned(FSession) then
          begin
            SetGUIControls;
            Caption := ApplicationCaption;
            Application.Title := ApplicationCaption;
          end;
        finally
          Params.Free;
        end;
      end;
    finally
      Form.Free;
    end;
  except
    raise;
  end;
end;

procedure TfrmMain.actnFileDisconnectExecute(Sender: TObject);
var
  i: Integer;

  procedure SetGUIControls;
  begin
    mnuMainWindow.Visible := False;
    mnuMainModule.Visible := False;
    tbNavigator.Visible := False;
    tbModule.Visible := False;
    actnFileDisconnect.Enabled := False;
    actnFileConnect.Enabled := True;
  end;

begin
  for i := MDIChildCount - 1 downto 0 do
    MDIChildren[i].Close;
  SetGUIControls;
  FSession := nil;
  Caption := ApplicationCaption;
  Application.Title := ApplicationCaption;
  cnMain.Close;
end;

procedure TfrmMain.actnFileExitExecute(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.actnNavigatorExecute(Sender: TObject);
begin
  if Assigned(ActiveForm) and (ActiveForm is TfrmDBBase) then
  begin
    with TfrmDBBase(ActiveForm) do
    begin
      if (Sender = actnDBFirst) then
        DBAction(daFirst)
      else if (Sender = actnDBPrior) then
        DBAction(daPrior)
      else if Sender = actnDBNext then
        DBAction(daNext)
      else if Sender = actnDBLast then
        DBAction(daLast)
      else if Sender = actnDBInsert then
        DBAction(daInsert)
      else if Sender = actnDBDelete then
        DBAction(daDelete)
      else if Sender = actnDBEdit then
        DBAction(daEdit)
      else if Sender = actnDBPost then
        DBAction(daPost)
      else if Sender = actnDBCancel then
        DBAction(daCancel)
      else if Sender = actnDBRefresh then
        DBAction(daRefresh)
      else if Sender = actnDBFilter then
        DBAction(daFilter);
    end;
  end;
end;

procedure TfrmMain.actnNavigatorUpdate(Sender: TObject);
begin
  if Assigned(ActiveForm) and (ActiveForm is TfrmDBBase) then
  begin
    with TfrmDBBase(ActiveForm) do
    begin
      if (Sender = actnDBFirst) then
        actnDBFirst.Enabled := daFirst in EnabledButtons
      else if (Sender = actnDBPrior) then
        actnDBPrior.Enabled := daPrior in EnabledButtons
      else if Sender = actnDBNext then
        actnDBNext.Enabled := daNext in EnabledButtons
      else if Sender = actnDBLast then
        actnDBLast.Enabled := daLast in EnabledButtons
      else if Sender = actnDBInsert then
        actnDBInsert.Enabled := daInsert in EnabledButtons
      else if Sender = actnDBDelete then
        actnDBDelete.Enabled := daDelete in EnabledButtons
      else if Sender = actnDBEdit then
        actnDBEdit.Enabled := daEdit in EnabledButtons
      else if Sender = actnDBPost then
        actnDBpost.Enabled := daPost in EnabledButtons
      else if Sender = actnDBCancel then
        actnDBCancel.Enabled := daCancel in EnabledButtons
      else if Sender = actnDBRefresh then
        actnDBRefresh.Enabled := daRefresh in EnabledButtons
      else if Sender = actnDBFilter then
        actnDBFilter.Enabled := daFilter in EnabledButtons;
    end;
  end
  else begin
    actnDBFirst.Enabled := False;
    actnDBPrior.Enabled := False;
    actnDBNext.Enabled := False;
    actnDBLast.Enabled := False;
    actnDBInsert.Enabled := False;
    actnDBDelete.Enabled := False;
    actnDBEdit.Enabled := False;
    actnDBPost.Enabled := False;
    actnDBCancel.Enabled := False;
    actnDBRefresh.Enabled := False;
    actnDBFilter.Enabled := False;
  end;
end;

constructor TfrmMain.Create(AOwner: TComponent);
begin
  inherited;
  CreateMutex(nil, False, 'mtxMain');
  Caption := ApplicationCaption;
  Application.Title := ApplicationCaption;
  Screen.OnActiveControlChange := ActiveControlChange;
  actnFileConnect.Enabled := True;
  actnFileDisconnect.Enabled := False;
  actnFileExit.Enabled := True;
  mnuMainModule.Visible := False;
  mnuMainWindow.Visible := False;
end;

destructor TfrmMain.Destroy;
begin
  Screen.OnActiveControlChange := nil;
  actnFileDisconnect.Execute;
  inherited;
end;

function TfrmMain.FindForm(FormClass: TfrmBaseClass; out Count: Integer): TfrmBase;
var
  i: Integer;
begin
  Count := 0;
  Result := nil;
  for i := MDIChildCount - 1 downto 0 do
  begin
    if MDIChildren[i].ClassType = FormClass then
    begin
      Inc(Count);
      Result := MDIChildren[i] as TfrmBase;
    end;
  end;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i: Integer;
begin
  for i := MDIChildCount - 1 downto 0 do
    MDIChildren[i].Close;
end;

function TfrmMain.GetActiveForm: TForm;
begin
  Result := Screen.ActiveForm;
end;

function TfrmMain.GetApplicationCaption: string;
const
  AppName = 'Poslovanje';
begin
  if Assigned(Session) then
    Result := Format('%s - %s', [AppName, Session.Params['POSLOVNO_LETO']])
  else
    Result := AppName;
end;

function TfrmMain.GetServer: IrdmMain;
begin
  Result := cnMain.GetServer as IrdmMain;
end;

function TfrmMain.ShowForm(FormClass: TfrmBaseClass; MaxCount: Integer): TfrmBase;
var
  Count: Integer;
begin
  Result := FindForm(FormClass, Count);
  if Count < MaxCount then
    Result := FormClass.Create(Self, FSession)
  else
    Result.BringToFront;
end;

end.
